 
  
  var provideCode="";
  var cityCode="";
  var townCode="";
  $(function(){
	  var result=parent.dataDetail;
 		loadBaseInfo(result);
 	    getProvince();
});
function loadBaseInfo(result){
	$("input[name='actualpersonSex'][value= '男']").attr("checked",true);	 
	 var id=result.dealerId;
  	 
	 $.ajax({
	        url:  hostUrl + "/dealer/searchDealerByDealerId",
	        type: 'post',//提交的方式
	        dataType:'json',
	        async:false,
	        data: {"dealerId":id},
	        success: function(obj) {
	        	if( obj.status==1){
	        		var result = obj.data;
  	        		if(result.legalpersonSex=='男'){
 	                   $("input[name='legalpersonSex'][value='男']").attr("checked",true);  
	        		}else{
	        			
	        			$("input[name='legalpersonSex'][value='女']").attr("checked",true);
	        		}
 	        		if(result.actualpersonSex=='男'){
 	        		   $("input[name='actualpersonSex'][value= '男']").attr("checked",true);    
 	        		}else{
 	        			
 	        			$("input[name='actualpersonSex'][value= '女']").attr("checked",true);    
 	        		}
	               	$("#legalpersonSex").val(result.legalperson_sex);
	        		$("#actualpersonSex").val(result.actualperson_sex);  
	        		$("#id").val(result.id);
	        		$("#dealerName").val(result.dealerName);
	        		$("#authorizationLimit").val(result.authorizationLimit);
	        		$("#stockAddress").val(result.stockAddress);
	        	    $("#legalpersonName").val(result.legalpersonName);
	        		$("#legalpersonPhone").val(result.legalpersonPhone);
	        		$("#actualpersonName").val(result.actualpersonName);
	         		$("#actualpersonPhone").val(result.actualpersonPhone); 
	          		 cityCode=result.cityCode; 
	          		 townCode=result.townCode;
	          		 provideCode=result.provinceCode; 
 					 
	        	} else {
	       		 layer.msg(obj.msg);
	       	}  
	        },
	        error:function(result) {
				Layer.showFailMsg("获取省市信息失败！稍后再试！");
	        }
		})
 
}
	
	 
  
 
//重新渲染表单
 function renderForm(){
   layui.use('form', function(){
    var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
    	  form.render();
   });
  }
 layui.use(['layer','form','element'], function(){
	    var layer = layui.layer,
	        $ = layui.jquery;
	    var form = layui.form;
	    form.on('select(selectCity)', function(data){
	        selectCity();
	    });
	    form.on('select(selectTown)', function(data){
	    	selectTown();
	    });
	    
	     form.on('submit(dealerEditSave)', function(data){
	    	 var sex = $("input[name='legalpersonSex']:checked").val();
	    	 if(typeof(sex) == 'undefined' || sex == ''){
	 		 Layer.showFailMsg("【法人性别】为必填字段");
	 		 return false;
	 	 }
	    	 
	    	 var sex = $("input[name='actualpersonSex']:checked").val();
	    	 if(typeof(sex) == 'undefined' || sex == ''){
	 		 Layer.showFailMsg("【实际控制人性别】为必填字段");
	 		 return false;
	 	    }
	    	 
	    	 $.ajax({
	 			cache: true,
	 			type: "POST",
	 			url:  "/dms-web/dealer/dealerEditSave",
	 			data: $('#dealer').serialize(),// 你的formid,获取所有值
	 			async: false,
	 			 
	 			success: function(data) { 
	 				if(data.status==0){
	 					 Layer.showSucMsg("修改成功");
	 					parent.layer.closeAll();
   					    parent.location.reload();
	 				}else{
	 					Layer.showFailMsg("修改失败");
	 				}
	 			 
	 					 
	 			},
	 			error: function(data) { 
	 		 		Layer.showFailMsg("操作失败");
	 		 	}
	 		}); 
	       return false;
	     }); 
	});
 //获取省
 function getProvince(){//   url: 'http://127.0.0.1:8080/dms-api/address/getAddress',
	 var pr =provideCode;
//	 var prss = $("#provinceList").val();
// 	  var pr = ${data.province_code};
	 	$.ajax({
	         url: serverUrl+'/dms-api/address/getAddress',
	         type: 'post',//提交的方式
	         dataType:'json',
	         success: function(data) {
	             //这是成功返回的数据，写自己的逻辑
	 			if(data.status==1){
	 				$("#provinceList").append('<option value="">请选择省</option>');
	 				$("#cityList option").remove();
	 				for(var i=0;i<data.data.length;i++){
	 					if(pr == data.data[i].code){
	 						$("#provinceList").append('<option selected value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
	 						selectCity();
	 					}else{
	 					$("#provinceList").append('<option value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
	 					}
	 				}
	 				renderForm();
	 			}else{
	 				Layer.showFailMsg("获取省市信息失败！稍后再试！");
	 			}
	         },
	         error:function(result) {
	         	Layer.showFailMsg("获取省市信息失败！稍后再试！");
	         }
	 	})
	 }
 function selectCity(){
	 var pr = "";
	 var prss = $("#provinceList").val();
	 if(prss == null){
		 pr = provideCode;
	 }else{
		 var prArr = prss.split(";");
		 pr = prArr[0];
	 }
		$.ajax({
	        url: serverUrl+'/dms-api/address/getAddress',
	        type: 'post',//提交的方式
	        dataType:'json',
	        data: {"provinceCode":pr},
	        success: function(data) {
	            //这是成功返回的数据，写自己的逻辑
				if(data.status==1){
					$("#cityList option").remove();
					$("#townList option").remove();
					$("#cityList").append('<option value="">请选择市</option>');
					for(var i=0;i<data.data.length;i++){
 				
						if(cityCode == data.data[i].code){
							$("#cityList").append('<option selected value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
							selectTown();
						}else{
							$("#cityList").append('<option value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
						}
					}
					renderForm();
					$("#townList").html("");
				}else{
					Layer.showFailMsg("获取省市信息失败！稍后再试！");
				}
	        },
	        error:function(result) {
	        	Layer.showFailMsg("获取省市信息失败！稍后再试！");
	        }
		})
	}

	function selectTown(){
		 var ci = "";
		 var citycode = $("#cityList").val();
		 if(citycode == null){
			 ci = cityCode;
		 }else{
			 var ciArr = citycode.split(";");
			 ci = ciArr[0];
		 }
		$.ajax({
	        url: serverUrl+'/dms-api/address/getAddress',
	        type: 'post',//提交的方式
	        dataType:'json',
	        data: {"cityCode":ci},
	        success: function(data) {
	            //这是成功返回的数据，写自己的逻辑
				if(data.status==1){
					$("#townList option").remove();
					for(var i=0;i<data.data.length;i++){
						if(townCode == data.data[i].code){
							$("#townList").append('<option selected value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
						}else{
							$("#townList").append('<option value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
						}
					}
					renderForm();
				}else{
					Layer.showFailMsg("获取省市信息失败！稍后再试！");
				}
	        },
	        error:function(result) {
				Layer.showFailMsg("获取省市信息失败！稍后再试！");
	        }
		})
	}
 
 renderForm();
   
   